<?php
/* 
 * The Install class is needed to create the table space
 * and put initial data like the first administrator into the database.
 *
 */
class Install {
    private $db = null;
    private $status = "no_connection";
    private $dbFactory = null;
    /**
     * The default constructor tries to connect to a database
     * and sets the internal status to connected if successful.
     */
    public function __construct() {
//        @include('../../layout/lookupClasses.inc');
//        $dbFacPath = LookupClassLocation::lookup("DBFac");
        @include(dba/dbFac.inc);
        $this->dbFactory = new DatabaseFactory();
        $this->db = $this->dbFactory->connect();
        if($this->db->isConnected()) {
           $status = "connected";
        }
    }
    /**
     * The parameters are forwarded to the DatabaseFactory and a
     * Database object is created.
     * If that was successful true is returned if not false.
     * @param Database $dbms
     * @param String $host
     * @param String $dbName
     * @param String $user
     * @param String $pass
     * @return boolean true if successful false otherwise.
     */
    public function testConnection($dbms,$host,$dbName,$user,$pass) {
        $dbms = strtolower($dbms);
        $this->dbFactory = new DatabaseFactory($dbms, $host, $dbName, $user, $pass);
        $db = $this->dbFactory->connect();
        if($db->isConnected()) {
            return(true);
        }
        return(false);
    }
    /**
     * The parameters currently present in the DatabaseFactory object
     * are written to the expected location of the settings file.
     * The function testConnection is checked before writing.
     * If the connection is not valid, nothing is written.
     * @return True if writing was successful, false otherwise.
     */
    public function writeSettings() {
        if(testConnection($this->dbFactory->getDbms(),$this->dbFactory->getHost(),$this->dbFactory->getDbName(),$this->dbFactory->getUser(),$this->dbFactory->getPass())) {
            $this->dbFactory->writeSettings();
            return(true);
        } else
            return(false);
    }
    /**
     * This installation assumes that there will be only one installation.
     * If a tablespace has already been created it will not attempt to recreate
     * or overwrite it. It would need to be deleted first.
     */
    public function createTableSpace() {
        if(!$db->tableSpaceExists())
            $db->createTableSpace();
    }
    /**
     * The installation assumes the present of only one (first) admin.
     * If an admin already exists a new one will not be created.
     * @param User $admin
     */
    public function addAdmin($admin) {
        if(!$db->hasAdmin()) {

            $groupsPath = LookupClassLocation::lookup("Groups");
            @include($groupsPath);
            $groupPath = LookupClassLocation::lookup("Group");
            @include($groupPath);
            
            $groups = new Groups();
            $group = $groups->getAdminGroup();
            if(!$group) {
                $groups->createDefaultGroups();
                $group = $groups->getAdminGroup();
            }
            $group->addUser($admin);
        }
    }
}
?>
